/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.config;

import com.vimhe.ezimp2.config.bean.ColumnConfig;
import com.vimhe.ezimp2.config.bean.Configuration;
import com.vimhe.ezimp2.config.bean.FileConfig;
import com.vimhe.ezimp2.config.bean.RowConfig;
import com.vimhe.ezimp2.config.bean.SheetConfig;
import com.vimhe.ezimp2.config.bean.ValidatorConfig;
import com.vimhe.ezimp2.config.exception.ConfigException;

import java.util.Map;

/**
 * 快速获取某个 DataBean 对应的配置对象
 *
 * @author Vimhe
 */
public final class ImportConfig {

    /**
     * 配置对象
     */
    private Configuration configuration;

    /**
     * 构造器
     *
     * @param configSource 配置源
     */
    public ImportConfig(ConfigSource configSource) {
        if (configSource == null) {
            throw new ConfigException("空的配置源");
        }

        configuration = configSource.read();
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    /**
     * 获取文件配置对象
     *
     * @param fileIndex 文件索引
     * @return 文件配置对象
     */
    public FileConfig getFileConfig(final String fileIndex) {
        Map<String, FileConfig> fileConfigMap = getConfiguration().getFileConfigMap();
        if (fileConfigMap.isEmpty()) {
            return null;
        }

        return fileConfigMap.get(fileIndex);
    }

    /**
     * 获取工作表配置对象
     *
     * @param fileIndex  文件索引
     * @param sheetIndex 工作表索引
     * @return 工作表配置对象
     */
    public SheetConfig getSheetConfig(final String fileIndex, final int sheetIndex) {
        FileConfig fileConfig = getFileConfig(fileIndex);
        if (fileConfig == null) {
            return null;
        }

        Map<Integer, SheetConfig> sheetConfigMap = fileConfig.getSheetConfigMap();
        if (sheetConfigMap.isEmpty()) {
            return null;
        }

        return sheetConfigMap.get(sheetIndex);
    }

    /**
     * 获取行配置对象
     *
     * @param fileIndex  文件索引
     * @param sheetIndex 工作表索引
     * @param rowIndex   行索引
     * @return 行配置对象
     */
    public RowConfig getRowConfig(final String fileIndex, final int sheetIndex, final int rowIndex) {
        SheetConfig sheetConfig = getSheetConfig(fileIndex, sheetIndex);
        if (sheetConfig == null) {
            return null;
        }

        RowConfig rowConfig = sheetConfig.getRowConfig();
        return (rowConfig == null)
                || (rowIndex < rowConfig.getStart() || rowIndex > rowConfig.getEnd())
                ? null : rowConfig;
    }

    /**
     * 获取单元格配置对象
     *
     * @param fileIndex   文件索引
     * @param sheetIndex  工作表索引
     * @param rowIndex    行索引
     * @param columnIndex 列索引
     * @return 单元格配置对象
     */
    public ColumnConfig getColumnConfig(final String fileIndex,
                                        final int sheetIndex,
                                        final int rowIndex,
                                        final String columnIndex) {
        RowConfig rowConfig = getRowConfig(fileIndex, sheetIndex, rowIndex);
        if (rowConfig == null) {
            return null;
        }

        Map<String, ColumnConfig> columnConfigMap = rowConfig.getColumnConfigMap();
        if (columnConfigMap.isEmpty()) {
            return null;
        }

        ColumnConfig columnConfig = columnConfigMap.get(columnIndex);
        return columnConfig == null || (!columnConfig.getReadable()) ? null : columnConfig;
    }

    /**
     * 获取验证器配置对象
     *
     * @param fileIndex     文件索引
     * @param sheetIndex    工作表索引
     * @param rowIndex      行索引
     * @param columnIndex   列索引
     * @param validatorName 验证器索引
     * @return 验证器配置对象
     */
    public ValidatorConfig getValidationConfig(final String fileIndex,
                                               final int sheetIndex,
                                               final int rowIndex,
                                               final String columnIndex,
                                               final String validatorName) {
        ColumnConfig columnConfig = getColumnConfig(fileIndex, sheetIndex, rowIndex, columnIndex);
        if (columnConfig == null) {
            return null;
        }

        return columnConfig.getValidatorConfigMap().get(validatorName);
    }

}
